package com.kyzh.core.pager.weal.live

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import com.gushenge.core.beans.RoomGift
import com.gushenge.core.dao.MMKVConsts
import com.kyzh.core.R
import com.kyzh.core.databinding.FragmentSendGiftItemBinding
import com.kyzh.core.databinding.ItemSendGiftItemBinding
import com.tencent.qcloud.tuicore.component.fragments.BaseFragment
import com.zhpan.bannerview.BannerViewPager
import com.zhpan.bannerview.BaseBannerAdapter
import com.zhpan.bannerview.BaseViewHolder
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle


class SendGiftItemFragment : BaseFragment() {
    private  val ARG_PARAM1 = "param1"
    private  val ARG_PARAM2 = "param2"
    private lateinit var banner:BannerViewPager<ArrayList<RoomGift>>
    private var position: Int = 0
    companion object {
        val TAG = "SendGift"
        @JvmStatic
        fun newInstance(param1: ArrayList<ArrayList<RoomGift>>,position: Int) =
            SendGiftItemFragment().apply {
                arguments = Bundle().apply {
                    putSerializable(ARG_PARAM1, param1)
                    putInt(ARG_PARAM2,position)
                }
            }
    }
    private var binding :FragmentSendGiftItemBinding? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentSendGiftItemBinding.inflate(inflater)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.initView()
    }
    fun FragmentSendGiftItemBinding.initView() {
       banner = root as BannerViewPager<ArrayList<RoomGift>>
       arguments?.let {
           (it.getSerializable(ARG_PARAM1) as ArrayList<ArrayList<RoomGift>>).let {
               banner.setAdapter(BannerAdapter())
                   .setIndicatorStyle(IndicatorStyle .ROUND_RECT)
                   .setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
                   .setAutoPlay(false)
                   .create(it)
           }
           position = it.getInt(ARG_PARAM2,0)
       }
    }


    private inner class BannerAdapter():BaseBannerAdapter<ArrayList<RoomGift>>(){
        override fun bindData(
            holder: BaseViewHolder<ArrayList<RoomGift>>,
            data: ArrayList<RoomGift>,
            position: Int,
            pageSize: Int
        ) {
            holder.findViewById<RecyclerView>(R.id.root).apply {
                layoutManager = object : GridLayoutManager(requireContext(),4){
                    override fun canScrollHorizontally(): Boolean  = false
                    override fun canScrollVertically(): Boolean  = false
                }
                val beans = arrayListOf<RoomGift>()
                beans.addAll(data)
                adapter =  GiftAdapter(beans)
            }
        }

        override fun getLayoutId(viewType: Int): Int  = R.layout.item_send_gift_banner

    }
    private inner class GiftAdapter(val beans:ArrayList<RoomGift>):BaseQuickAdapter<RoomGift,BaseDataBindingHolder<ItemSendGiftItemBinding>>
        (R.layout.item_send_gift_item,beans){
        override fun convert(
            holder: BaseDataBindingHolder<ItemSendGiftItemBinding>,
            item: RoomGift
        ) {
            holder.dataBinding?.apply {
                this.data = item
                root.setBackgroundResource(if (item.select) R.drawable.bg_border_8dp_cfcfcf else android.R.color.transparent)

                holder.itemView.setOnClickListener {
                    val selects = arrayListOf<Int>()
                    beans.forEachIndexed { index, gift ->
                        if (gift.select) selects.add(index)

                        gift.select = false
                    }
                    if (!selects.contains(holder.layoutPosition)) selects.add(holder.layoutPosition)
                    beans[holder.layoutPosition].select = !item.select
                    val selectlist = Gson().fromJson<ArrayList<RoomGift>>(
                        MMKVConsts.selectGiftList,
                        object: TypeToken<ArrayList<RoomGift>>() {
                        }.type
                    )
                    selectlist.removeAt(position)
                    selectlist.add(position,item)
                    MMKVConsts.selectGiftList =  Gson().toJson(selectlist)

                    selects.forEach {
                        notifyItemChanged(it)
                    }
                }
            }
        }
    }
}